import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

// class PortalBullet define the portal bullet and its movement
public class PortalBullet {

	private int aim;
	private Point3D vec;
	private Point3D org;
	float scalar=0.02f;

	float leftWall;
	float rightWall;
	float forwardWall;
	float backWall;
	float celling;
	float floor;
	
	Portal portal;
	int red;
	int green;
	int blue;
	int gate ;
	
	GLU glu;
	
	public PortalBullet(GLU glu, Portal portal,int r,int g,int b,int gate) {
		this.portal = portal;
		red=r;
		green=g;
		blue=b;
		this.gate=gate;
		this.glu = glu;
	}
	public void shoot (Point3D lookat , Point3D eye,LevelBuilder l1){
		vec= new Point3D();
		vec.x=lookat.x-eye.x;
		
		vec.y=lookat.y-eye.y;
		vec.z=lookat.z-eye.z;
		org= new Point3D(eye.x, eye.y, eye.z);
		
		leftWall=l1.leftWall;
		rightWall=l1.rightWall;
		forwardWall=l1.forwardWall;
		backWall=l1.backWall;
		celling=l1.celling;
		floor=l1.floor;

		
	}

	public void draw(GL gl) {
	
 		if (vec!=null){
			gl.glPushMatrix();
			gl.glTranslated(org.x,org.y,org.z);
			
			
			gl.glCallList(aim);
			gl.glPopMatrix();
			

			
			if (org.z >forwardWall){
				buildPortal(org,1);
			}else if (org.z<backWall){
				buildPortal(org,2);
			}else if (org.x<rightWall){
				buildPortal(org,3);
			}else if (org.x>leftWall){
				buildPortal(org,4);
			}else if (org.y<floor){
				buildPortal(org,5);
			}else if (org.y>celling){
				buildPortal(org,6);
			}else{
			//	scalar+=0.8;
				org.x+=vec.x;
				org.y+=vec.y;
				org.z+=vec.z;
			}
		}

		

	}
	private void buildPortal(Point3D org2, int wall) {
		vec=null;
		org=null;
		scalar=0.02f;
		
		portal.openPortal(gate,org2,wall);
		
	}
	public void buildBullet(GL gl) {
		aim = gl.glGenLists(1);

		gl.glNewList(aim, GL.GL_COMPILE);

		// draw top
		gl.glColor3f(red, green, blue);
		
		GLUquadric bullet = glu.gluNewQuadric();
		glu.gluQuadricTexture(bullet, true);
		glu.gluQuadricDrawStyle(bullet, GLU.GLU_FILL);
		glu.gluQuadricNormals(bullet, GLU.GLU_FLAT);
		glu.gluQuadricOrientation(bullet, GLU.GLU_OUTSIDE);
		glu.gluSphere(bullet, 2f, 16, 16);
		gl.glColor3f(1, 1, 1);
		gl.glEndList();
		
	}


}

